package com.ycp.erp.warehouse.ui.activity;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.jakewharton.rxbinding2.view.RxView;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.view.adapter.PagerFragmentAdapter;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.base.BaseFragment;
import com.one.common.view.multitytype.MultiTypeAdapter;
import com.one.common.view.slidingtab.ScrollViewPagerAdapter;
import com.one.common.view.slidingtab.SlidingTabLayout;
import com.one.common.view.widget.ClearEditView;
import com.one.common.view.widget.MyTitleBar;
import com.ycp.erp.R;
import com.ycp.erp.warehouse.model.bean.ProcurementWarehouse;
import com.ycp.erp.warehouse.presenter.WarehousePresenter;
import com.ycp.erp.warehouse.ui.binder.ProcyrementWarehouseSelectBinder;
import com.ycp.erp.warehouse.ui.fragment.MoveWareHouseFragment;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;

/**
 * Class Des:商品移库
 * Created by bjh on 2021/9/6.
 */
@Route(path = RouterPath.MOVE_WAREHOUSE)
public class MoveWarehouseActivity extends BaseActivity<WarehousePresenter> {
    @BindView(R.id.slide)
    SlidingTabLayout mSlidTab;
    @BindView(R.id.vp_order)
    ViewPager vpOrder;
    @BindView(R.id.ce)
    ClearEditView ce;
    @BindView(R.id.vMask)
    View vMask;
    @BindView(R.id.rcvSelect)
    RecyclerView rcvSelect;
    @BindView(R.id.clShow)
    ConstraintLayout clShow;
    @BindView(R.id.tvGoodsTypeTotal)
    TextView tvGoodsTypeTotal;
    @BindView(R.id.tvGoodsTotal)
    TextView tvGoodsTotal;
    @BindView(R.id.rlBottom)
    RelativeLayout rlBottom;
    @BindView(R.id.tvSelectTypeGoodsNum)
    TextView tvSelectTypeGoodsNum;
    @BindView(R.id.llSelect)
    LinearLayout llSelect;
    @BindView(R.id.tvConfirm)
    TextView tvConfirm;
    @BindView(R.id.tvFilterwarehouse)
    TextView tvFilterwarehouse;
    @BindView(R.id.ivFilterWarehouseTriangle)
    ImageView ivFilterWarehouseTriangle;
    @BindView(R.id.rlFilterwarehouse)
    RelativeLayout rlFilterwarehouse;
    @BindView(R.id.tvFilterGoods)
    TextView tvFilterGoods;
    @BindView(R.id.ivFilterGoodsTriangle)
    ImageView ivFilterGoodsTriangle;
    @BindView(R.id.rlFilterGoods)
    RelativeLayout rlFilterGoods;
    private String[] tabs = {"全部商品", "示例分类名称", "手机", "啤酒类", "零食"};
    private PagerFragmentAdapter pagerAdapter;
    private MultiTypeAdapter adapter;
    private ArrayList<String> arrayList = new ArrayList<>();
    private ArrayList<String> arrayListGoods = new ArrayList<>();

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("商品移库").hideLine();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_move_warehouse;
    }

    @SuppressLint("CheckResult")
    @Override
    public void initView() {
        super.initView();
        initPageTitle();
        LinearLayoutManager scrollLinearLayoutManager = new LinearLayoutManager(getContext());
        rcvSelect.setLayoutManager(scrollLinearLayoutManager);
        adapter = new MultiTypeAdapter();
        ProcyrementWarehouseSelectBinder mBinder = new ProcyrementWarehouseSelectBinder();
        adapter.register(ProcurementWarehouse.class, mBinder);
        rcvSelect.setAdapter(adapter);
        adapter.setItems(getData());
        adapter.notifyDataSetChanged();
        arrayList.add("全部库房");
        arrayList.add("库房库房1");
        arrayList.add("库房库房2");
        arrayList.add("库房库房2");
        arrayList.add("库房库房2");
        arrayList.add("全部库房");
        arrayListGoods.add("全部商品");
        arrayListGoods.add("根据商品名排序");
        arrayListGoods.add("只看有库存商品");
        arrayListGoods.add("只看预警商品");
        arrayListGoods.add("只看有正常商品");
        rlBottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rlBottom.isSelected()) {
                    clShow.setVisibility(View.GONE);
                } else {
                    clShow.setVisibility(View.VISIBLE);
                }
                rlBottom.setSelected(!rlBottom.isSelected());
            }
        });
        vMask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clShow.setVisibility(View.GONE);
                rlBottom.setSelected(false);
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterManager.getInstance().go(RouterPath.CONFIRM_MOVE_WAREHOUSE);
            }
        });
        RxView.clicks(rlFilterwarehouse).throttleFirst(100, TimeUnit.MILLISECONDS).subscribe(o -> {
            //选择库房
            OptionsPickerView<String> build = new OptionsPickerBuilder(this, (options1, options2, options3, v) ->
                    tvFilterwarehouse.setText("当前库房：" + arrayList.get(options1))).setSubmitColor(getResources().getColor(R.color.root_blue))
                    .setCancelColor(getResources().getColor(R.color.gray_d6))
                    .setLineSpacingMultiplier(2.8f)
                    .build();
            build.setPicker(arrayList);
            build.show();
        });
        RxView.clicks(rlFilterGoods).throttleFirst(100, TimeUnit.MILLISECONDS).subscribe(o -> {
            //赛选商品
            OptionsPickerView<String> build = new OptionsPickerBuilder(this, (options1, options2, options3, v) ->
                    tvFilterGoods.setText("筛选：" + arrayListGoods.get(options1))).setSubmitColor(getResources().getColor(R.color.root_blue))
                    .setCancelColor(getResources().getColor(R.color.gray_d6))
                    .setLineSpacingMultiplier(2.8f)
                    .build();
            build.setPicker(arrayListGoods);
            build.show();
        });
    }

    /**
     * 模拟数据
     *
     * @return
     */
    private ArrayList<ProcurementWarehouse> getData() {
        ArrayList<ProcurementWarehouse> procurementWarehouses = new ArrayList<>();
        ProcurementWarehouse procurementWarehouse = new ProcurementWarehouse();
        procurementWarehouse.setGoodsName("测试");
        procurementWarehouse.setGoodsDesc("测试描述");
        procurementWarehouse.setGoodsNum(0);
        procurementWarehouse.setGoodsPrice("100");
        procurementWarehouses.add(procurementWarehouse);
        ProcurementWarehouse procurementWarehouse1 = new ProcurementWarehouse();
        procurementWarehouse1.setGoodsName("测试");
        procurementWarehouse1.setGoodsDesc("测试描述");
        procurementWarehouse1.setGoodsNum(0);
        procurementWarehouse1.setGoodsPrice("100");
        procurementWarehouses.add(procurementWarehouse1);
        ProcurementWarehouse procurementWarehouse2 = new ProcurementWarehouse();
        procurementWarehouse2.setGoodsName("测试");
        procurementWarehouse2.setGoodsDesc("测试描述");
        procurementWarehouse2.setGoodsNum(0);
        procurementWarehouse2.setGoodsPrice("100");
        procurementWarehouses.add(procurementWarehouse2);
        return procurementWarehouses;
    }

    /**
     * 初始化头部
     */
    private void initPageTitle() {
        ArrayList<BaseFragment> taskFragments = getTaskFragments();
        pagerAdapter = new PagerFragmentAdapter(getSupportFragmentManager(), taskFragments);
        vpOrder.setAdapter(pagerAdapter);
        vpOrder.setOffscreenPageLimit(5);
        ScrollViewPagerAdapter adapter = new ScrollViewPagerAdapter(getSupportFragmentManager(), taskFragments, tabs);
        vpOrder.setOffscreenPageLimit(taskFragments.size());
        vpOrder.setAdapter(adapter);
        mSlidTab.setViewPager(vpOrder);

    }

    /**
     * 各种状态的任务列表
     */
    private ArrayList<BaseFragment> getTaskFragments() {
        ArrayList<BaseFragment> fragments = new ArrayList<>();
        fragments.add(MoveWareHouseFragment.getInstance("0"));
        fragments.add(MoveWareHouseFragment.getInstance("1"));
        fragments.add(MoveWareHouseFragment.getInstance("2"));
        fragments.add(MoveWareHouseFragment.getInstance("3"));
        fragments.add(MoveWareHouseFragment.getInstance("4"));
        return fragments;
    }


}
